package com.bdqn.oa.dao.impl;

import com.bdqn.oa.dao.WorkplanDao;
import com.bdqn.oa.entity.Workplan;
import com.bdqn.oa.entity.Workreport;
import com.bdqn.oa.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.List;

/**
 * @author Zhouxw
 * @date 2022/7/21 17:20
 */
public class WorkplanDaoImpl implements WorkplanDao {
    QueryRunner queryRunner = new QueryRunner();

    /**
     * 新增一条工作计划数据
     *
     * @param workplan 工作计划
     * @return 影响行数
     */
    @Override
    public int saveWorkplan(Workplan workplan) throws SQLException {
        String sql = "insert into workplan (employee_id, date, start_time, end_time, work_name, work_content, iscore, `status`) " +
                " value (?, ?, ?, ?, ?, ?, ?, ?)";
        int result = queryRunner.update(DruidUtils.getConnection(), sql, workplan.getEmployeeId(), workplan.getDate(),
                workplan.getStartTime(), workplan.getEndTime(), workplan.getWorkName(), workplan.getWorkContent(),
                workplan.getIsCore(), workplan.getStatus());
        return result;
    }

    /**
     * 更新工作计划数据(主要更新状态和点评id)
     *
     * @param workplan 工作计划
     * @return 影响行数
     */
    @Override
    public int updateWorkplan(Workplan workplan) {
        return 0;
    }

    /**
     * 根据员工id和日期更新工作计划状态
     * 状态 1.已保存2.已提交3.已通过 4.已驳回
     * **注意参数为LocalDate类型,注意参数为LocalDate类型,注意参数为LocalDate类型**
     *
     * @param employeeId 员工id
     * @param date       日期
     * @param status     状态
     * @return 影响行数
     */
    @Override
    public int updateStatus(Integer employeeId, LocalDate date, Integer status) throws SQLException {
        String sql = "update workplan set `status` = ? where employee_id = ? and date = ?";
        int result = queryRunner.update(DruidUtils.getConnection(), sql, status, employeeId, date);
        return result;
    }

    /**
     * 根据员工id和日期更新工作计划点评
     * **注意参数为LocalDate类型,注意参数为LocalDate类型,注意参数为LocalDate类型**
     *
     * @param employeeId 员工id
     * @param date       日期
     * @param commentId  点评id
     * @return 影响行数
     */
    @Override
    public int updateComment(Integer employeeId, LocalDate date, Integer commentId) throws SQLException {
        String sql = "update workplan set comment_id = ? where employee_id = ? and date = ?";
        int result = queryRunner.update(DruidUtils.getConnection(), sql, commentId, employeeId, date);
        return result;
    }

    /**
     * 通过workplan_id更新某一条计划的完成状态
     * 1.未完成 2.已完成
     *
     * @param id         计划id
     * @param finishFlag 完成状态
     * @return 影响行数
     */
    @Override
    public int updateFinishFlag(Integer id, Integer finishFlag) throws SQLException {
        String sql = "update workplan set finish_flag = ? where id = ?";
        int result = queryRunner.update(DruidUtils.getConnection(), sql, finishFlag, id);
        return 0;
    }

    /**
     * 通过id查询工作计划
     *
     * @param id 工作计划id
     * @return 工作计划
     */
    @Override
    public Workplan getWorkplanById(Integer id) throws SQLException {
        String sql = "select * from workplan where id = ?";
        Workplan workplan = queryRunner.query(DruidUtils.getConnection(), sql, new BeanHandler<>(Workplan.class), id);
        return workplan;
    }

    /**
     * 通过时间范围查询及员工id查询工作计划
     * 查询某一天工作工作计划需要startDate==endDate
     * **注意参数为LocalDate类型,注意参数为LocalDate类型,注意参数为LocalDate类型**
     *
     * @param startDate  起始时间
     * @param endDate    结束时间
     * @param employeeId 员工id
     * @return 工作计划集合
     * @throws SQLException 数据库异常
     */
    @Override
    public List<Workplan> listWorkplanByDateScope(LocalDate startDate, LocalDate endDate, Integer employeeId) throws SQLException {
        String sql = "select * from workplan where employee_id = ? and date between ? and ?";
        List<Workplan> workplanList = queryRunner.query(DruidUtils.getConnection(), sql, new BeanListHandler<>(Workplan.class), employeeId, startDate, endDate);
        return workplanList;
    }
}
